﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.COM;
using WCFMINERP.Factory.Presenter;
using WCFMINERP.InnerFactoryContracts.DataContracts;

namespace WCFMINERP.Factory
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        LoginPresenter _presenter = new LoginPresenter();

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUser.Text = Properties.Settings.Default.UserName;
            txtPwd.Text = Properties.Settings.Default.UserPwd;

         
            checkBoxRememnerUserName.Checked = Properties.Settings.Default.IsRememberUseName;
            checkBoxRememberPwd.Checked = Properties.Settings.Default.IsRememberPwd;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUser.Text))
            {
                MessageBox.Show("请输入用户名");
                return;
            }

            if (string.IsNullOrEmpty(txtPwd.Text))
            {
                MessageBox.Show("请输入密码");
                return;
            }
            try
            {
               
                UserInfo userInfo  = _presenter.Login(txtUser.Text.Trim(), Md5HashCode.GetHash(txtPwd.Text.Trim()));
               
                if (userInfo != null)
                {
                    InnterManagePresenter.UserInfo = userInfo;
                    if (checkBoxRememnerUserName.Checked)
                    {
                        Properties.Settings.Default.IsRememberUseName = true;
                        Properties.Settings.Default.UserName = txtUser.Text;
                    }
                    else
                    {
                        Properties.Settings.Default.IsRememberUseName = false;
                        Properties.Settings.Default.UserName = "";
                    }
                    if (checkBoxRememberPwd.Checked)
                    {
                        Properties.Settings.Default.IsRememberPwd = true;
                        Properties.Settings.Default.UserPwd = txtPwd.Text;
                    }
                    else
                    {
                        Properties.Settings.Default.IsRememberPwd = false;
                        Properties.Settings.Default.UserPwd = "";
                    }
                    Properties.Settings.Default.Save();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("输入的用户名称或密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                if (ex is EndpointNotFoundException)
                {
                    MessageBox.Show("连接服务器失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }



        }

        


        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Hide();
        }


        /// <summary>
        /// 检查输入信息
        /// </summary>
        /// <returns></returns>
        private int CheckInputData()
        {
            int ret = 0;

            if (string.IsNullOrEmpty(txtUser.Text.Trim()))
            {
                ret = 1;
            }
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                ret = 2;
            }
            return ret;
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Environment.Exit(0);
        }

        /// <summary>
        /// 设置岗位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_Setts_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获取输入的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtUser_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

     

     
    }
}
